package cn.newgrand.netcall.imp.advertisement;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.Workspace;
import org.jivesoftware.spark.plugin.Plugin;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ChatRoomListenerAdapter;

import cn.newgrand.netcall.advertisement.AdXML;
import cn.newgrand.netcall.advertisement.AdvertisementManager;
/**
 * 广告类插件
 * 
 * @author SidneyRen
 *
 */
public class AdvertisementPlugin implements Plugin {
	
	AdMainWin adMainWin ;
	AdChatTop adChatTop ;
	AdChatBottom adChatBottom ;
	Workspace workspace;
	ChatRoom chatRoom ;

	@Override
	public void initialize() {
		AdXML adXml = AdXML.getInstance();
		
		initAdMainWin();
		
		//广告面板直接在chatRoom和chatAreaSendField中线程添加
//		initAdChatTop();
//		initAdChatBottom();

		
	}
	
	public void initAdMainWin(){
		
		// 加载广告类
		adMainWin = AdvertisementManager.getInstance().getAdMainWin();
		workspace = SparkManager.getWorkspace();
		workspace.add(adMainWin,
				new GridBagConstraints(0, 10, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		workspace.invalidate();
		workspace.validate();
		workspace.repaint();
	}
	public void initAdChatTop(){
		
		adChatTop = AdvertisementManager.getInstance().getAdChatTop();
		SparkManager.getChatManager().addChatRoomListener(new ChatRoomListenerAdapter() {
			public void chatRoomOpened(final ChatRoom room) {
				room.getToolBar().add(adChatTop, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
			}
			public void chatRoomActivated(ChatRoom room) {
//				adChatTop.updateAd();
				room.getToolBar().add(adChatTop, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		    }
		});
	}
	public void initAdChatBottom(){
		
		adChatBottom = AdvertisementManager.getInstance().getAdChatBottom();
		
		SparkManager.getChatManager().addChatRoomListener(new ChatRoomListenerAdapter() {
			public void chatRoomOpened(final ChatRoom room) {
				room.getChatAreaSendField().add(adChatBottom, new GridBagConstraints(0, 1, 1, 1, 1.0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
			}
			public void chatRoomActivated(ChatRoom room) {
				room.getChatAreaSendField().add(adChatBottom, new GridBagConstraints(0, 1, 1, 1, 1.0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
			 }
		});
	}


	@Override
	public boolean canShutDown() {
		return false;
	}
	
	@Override
	public void shutdown() {
		
	}

	@Override
	public void uninstall() {
		
	}
	
}
